home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 6.4 KB | 222 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWMemorH.h
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWMEMORH_H
- #define FWMEMORH_H
-
- #ifndef FWPLATME_H
- #include <FWPlatMe.h>
- #endif
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
-
- //========================================================================================
- // Forward class declarations
- //========================================================================================
-
- class FW_CMemoryHookList;
-
-
- //========================================================================================
- // Type definitions
- //========================================================================================
-
- typedef unsigned long FW_BytePtr;
- typedef unsigned long FW_BlockSize;
-
- #ifdef FW_DEBUG
- //========================================================================================
- // FW_CMemoryHook (FW_DEBUG only)
- //
- // A Memory hook that can be used to track heap operations by subclassing FW_CMemoryHook
- // and overriding the methods of interest. Your subclass is then registered with a
- // FW_CMemoryHeap.
- //
- //========================================================================================
-
- class FW_CMemoryHook
- {
- public:
- // These methods should all be abstract, but FW_CMemoryHookList instantiates a
- // FW_CMemoryHook to use as the head of its linked list, so here we just use empty
- // inlines. A FW_CMemoryHook still cannot be instantiated because the constructor
- // is protected.
-
- virtual FW_BlockSize AboutToAllocate(FW_BlockSize size);
- virtual void* DidAllocate(void* blk, FW_BlockSize);
- virtual void* AboutToBlockSize(void* blk);
- virtual void* AboutToFree(void* blk);
- virtual void AboutToRealloc(void*& , FW_BlockSize&);
- virtual void* DidRealloc(void* blk, FW_BlockSize);
- virtual void AboutToReset();
- virtual void Comment(const char*);
-
- virtual~ FW_CMemoryHook();
-
- protected:
- FW_CMemoryHook();
-
- private:
- FW_CMemoryHook* fNextHook, * fPreviousHook;
-
- friend FW_CMemoryHookList;
-
- FW_CMemoryHook(const FW_CMemoryHook& blk);
- FW_CMemoryHook& operator=(const FW_CMemoryHook& blk);
- // This class shouldn't be copied.
- };
- #endif
-
-
- #ifdef FW_DEBUG
- //========================================================================================
- // FW_CMemoryHookList (FW_DEBUG only)
- //
- // A list of memory hooks. This is a special linked list that assumes the links for
- // the list are fields of FW_CMemoryHook. This avoids endless recursion were we to
- // allocate nodes for the MemoryHooks here.
- //
- //========================================================================================
-
- class FW_CMemoryHookList
- {
- public:
- FW_CMemoryHookList();
-
- void Add(FW_CMemoryHook* aMemoryHook);
- void Remove(FW_CMemoryHook* aMemoryHook);
- FW_CMemoryHook* First();
- FW_CMemoryHook* Next();
- FW_CMemoryHook* Previous();
- FW_CMemoryHook* Last();
-
- ~FW_CMemoryHookList();
-
- private:
- FW_CMemoryHook fHead;
- FW_CMemoryHook* fCurrentHook;
-
- FW_CMemoryHookList(const FW_CMemoryHookList& blk);
- FW_CMemoryHookList& operator=(const FW_CMemoryHookList& blk);
- // This class shouldn't be copied.
- };
- #endif
-
-
- //========================================================================================
- // FW_CMemoryHeap
- //
- // Abstract base class for memory heaps.
- //
- //========================================================================================
-
- class FW_CMemoryHeap
- {
- public:
- enum { kBlockTypeId = 0 };
-
- static FW_CMemoryHeap* fHeapList;
- static FW_CMemoryHeap* GetFirstHeap();
-
- void* Allocate(FW_BlockSize size);
- FW_BlockSize BlockSize(const void* block) const;
- virtual unsigned long BytesAllocated() const;
- virtual unsigned long BytesFree() const = 0;
- void Free(void*);
- virtual FW_CMemoryHeap* GetNextHeap() const;
- virtual Boolean GetZapOnAllocate() const;
- virtual Boolean GetZapOnFree() const;
- virtual unsigned long HeapSize() const = 0;
- virtual unsigned long NumberAllocatedBlocks() const;
- void Reset();
- void* Reallocate(void* block, FW_BlockSize newSize);
- virtual void SetZapOnAllocate(Boolean = false);
- virtual void SetZapOnFree(Boolean = false);
-
- void* operator new(SIZE_T size);
- void operator delete(void* ptr);
-
- virtual~ FW_CMemoryHeap();
-
- #ifdef FW_DEBUG
- static const char* kDefaultDescription;
- virtual const char* GetDescription() const;
- virtual void SetDescription(const char* description = kDefaultDescription);
-
- virtual void InstallHook(FW_CMemoryHook* FW_CMemoryHook);
- virtual void RemoveHook(FW_CMemoryHook* FW_CMemoryHook);
-
- virtual Boolean GetAutoValidation() const;
- virtual void SetAutoValidation(Boolean = false);
- Boolean IsValidBlock(void* blk) const;
- virtual Boolean IsMyBlock(void* blk) const = 0;
-
- virtual void Check() const = 0;
- virtual void Print(char* msg = "") const = 0;
- #endif
-
- protected:
- FW_CMemoryHeap(Boolean autoValidation = false,
- Boolean zapOnAllocate = true,
- Boolean zapOnFree = false);
-
- virtual void* AllocateRawMemory(FW_BlockSize size);
- virtual void* DoAllocate(FW_BlockSize size, FW_BlockSize& allocatedSize) = 0;
- virtual FW_BlockSize DoBlockSize(const void* block) const = 0;
- virtual void DoFree(void*) = 0;
- virtual void DoReset() = 0;
- virtual void* DoReallocate(void* block, FW_BlockSize newSize, FW_BlockSize& allocatedSize);
- virtual void FreeRawMemory(void* ptr);
-
- #ifdef FW_DEBUG
- virtual Boolean DoIsValidBlock(void* blk) const = 0;
- virtual void CompilerCheck();
- #endif
-
- private:
- FW_CMemoryHeap* fNextHeap;
- Boolean fZapOnAllocate;
- Boolean fZapOnFree;
- Boolean fAutoValidation;
- unsigned long fBytesAllocated;
- unsigned long fNumberAllocatedBlocks;
-
- #ifdef FW_DEBUG
- enum { kDescriptionLength = 32 };
- char fDescription[kDescriptionLength];
- FW_CMemoryHookList fMemoryHookList;
-
- FW_BlockSize CallAboutToAllocateHooks(FW_BlockSize size);
- void* CallDidAllocateHooks(void* blk, FW_BlockSize size);
- void* CallAboutToFW_BlockSizeHooks(void* blk);
- void* CallAboutToFreeHooks(void* blk);
- void CallAboutToReallocHooks(void*& blk, FW_BlockSize& size);
- void* CallDidReallocHooks(void* blk, FW_BlockSize size);
- void CallAboutToResetHooks();
- void CallCommentHooks(const char* comment);
- void ValidateAndReport(void* blk) const;
- #endif
-
- FW_CMemoryHeap(const FW_CMemoryHeap& blk);
- FW_CMemoryHeap& operator=(const FW_CMemoryHeap& blk);
- // This class shouldn't be copied.
- };
-
-
- #endif
-